import axios from "axios";
import store from "./../../store/module/global";
import Token from "./cookie";
import { stringify } from "./codeqs.js";

const CancelToken = axios.CancelToken;
let cancel;

// 创建axios实例
const axiosservice = axios.create({
	timeout: 600000, // 请求超时时间
	retry: 4, //重新请求次数
	retryDelay: 1000, //重新请求的间隔
	cancelToken: new CancelToken(function executor(c) {
		// executor 函数接收一个 cancel 函数作为参数
		cancel = c;
	}),
});

axiosservice.interceptors.request.use(
	(config) => {
		// let Tokens = new Token(112)
		// console.error(Tokens)
		// Tokens.saveToLocal()
		// let user = Token.getRegion()

		// if (!user) {
		// 	console.error(1111)
		// 	window.location.href = ( window.location.origin+'/relation-web/login')
		// 	return

		//   }

		config.withCredentials = false; // 允许携带token ,这个是解决跨域产生的相关问题
		config.headers = {
			"Content-Type": "application/json;charset=UTF-8",
		};
		if(config?.data?.file){
			config.headers = {
				'Content-Type': 'multipart/form-data'
			  }
		}
		// if(config.method==='get'&&typeof(config.data)==='boolean'){
		// 调用其他地方接口需要权限token
		// config.headers.loginDeviceType = 2; //设备为手机

		if (config?.data?.ghz) {
			config.headers["Content-Type"] = "application/x-www-form-urlencoded";
			config.data = stringify({ jsonString: JSON.stringify(config.data) });
		}
		let tokenList = JSON.parse(localStorage.getItem("userInfo")) || {};
		config.headers["Token"] = tokenList.token;
		// }
		return config;
	},
	(error) => {
		console.error("request：", error);
		return Promise.reject(error);
	}
);

axiosservice.interceptors.response.use(
	function (res) {
		return res;
	},
	function (err) {
		console.error("response: ", err);
		return Promise.reject(err);
	}
);

export default {
	async PostJson(url, data) {
		// data['dicVersion'] = store.state.version.dicVersion||null;
		const urlParams = new URLSearchParams(window.location.search);
		const dicVersion = urlParams.get('dicVersion'); 
		let falg = url.indexOf("?") === -1;
		console.error('PostJson',falg,dicVersion)

		if (falg) {
			url = url + "?dicVersion=" + (store.state.version.dicVersion|| dicVersion || null);
		} else {
			if (url.indexOf("&") != -1) {
				url = url + "&dicVersion=" + (store.state.version.dicVersion ||dicVersion|| null);
			} else {
				url = url + "&dicVersion=" + (store.state.version.dicVersion ||dicVersion|| null);
			}
		}
		var res = await axiosservice({ url, data, method: "post" });
		try {
			if (typeof res.data == "object") {
				return res.data;
			}
		} catch (error) {
			throw new Error("接口返回失败：" + res.data);
		}
	},
	async GetJson(url, isOther) {
		let falg = url.indexOf("?") === -1;

		if (falg) {
			url = url + "?dicVersion=" + (store.state.version.dicVersion || null);
		} else {
			if (url.indexOf("&") != -1) {
				url = url + "&dicVersion=" + (store.state.version.dicVersion || null);
			} else {
				url = url + "&dicVersion=" + (store.state.version.dicVersion || null);
			}
		}

		var res = await axiosservice({ url, method: "get", data: isOther });
		try {
			if(res.status == 401){
				return window.location.href = window.location.origin
			}
			if (typeof res.data == "object") {
				return res.data;
			}
		} catch (error) {
			throw new Error("接口返回失败：" + res.data);
		}
	},
	axios: axiosservice,
};
